using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Roles = Medianamik.Core.Security.Roles;
using Medianamik.Core.Cache;
using Medianamik.Globalization;
using Medianamik.Core;

namespace Medianamik.UI.SecurityManagement
{
    public partial class RoleManagement : BasePage
    {
        protected const string LIST_PAGE = "RoleList.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindControls();
            }
        }
        protected void cvRoleName_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (string.IsNullOrEmpty(args.Value))
            {
                args.IsValid = false;
                cvRoleName.ErrorMessage = Resources("Medianamik_SecurityManagement", "ErrorEmptyRoleName");
            }
            else if (Roles.RoleExists(args.Value))
            {
                args.IsValid = false;
                cvRoleName.ErrorMessage = Resources("Medianamik_SecurityManagement", "ErrorRoleAlreadyExists");
            }
            else
            {
                args.IsValid = true;
            }

        }


        protected void Save_Click(object sender, EventArgs e)
        {
            //TODO:Validation

            if (Page.IsValid)
            {
                string roleName = RoleParam;
                var logger = new Core.Logging.ActivityLogger();
                string message = string.Empty;
                if (IsNew)
                {
                    roleName = txtRoleName.Text;
                    Roles.CreateRole(roleName);
                    message = "Role Created";
                }
                else
                {
                    message = "Role Modified";
                }
                logger.Log(new Core.Logging.LoggableObject(roleName, Core.Logging.LogEntryType.Role), message);

                var selectedGroups = chkGroups.Items.Cast<ListItem>()
                    .Where(i => i.Selected).Select(i => i.Value);
								
                var notSelectedGroups = chkGroups.Items.Cast<ListItem>()
                    .Select(i => i.Value).Except(selectedGroups);

                Roles.RemoveRoleFromGroups(roleName,notSelectedGroups.ToArray());
                Roles.AddRoleToGroups(roleName, selectedGroups.ToArray());

                Translator.CreateResource("Medianamik_SecurityManagement", "Role_" + roleName, RoleTranslator.Values);
                
                Response.Redirect(LIST_PAGE);
            }
        }

        private void BindControls()
        {
          
            if (IsNew)
            {
                mvRoleName.SetActiveView(vwEnabled);
            }
            else
            {
                mvRoleName.SetActiveView(vwDisabled);
                chkGroups.SelectedGroups = Roles.GetGroupsInRole(RoleParam);
                RoleTranslator.Values = Translator.GetResources("Medianamik_SecurityManagement",
                    "Role_" + RoleParam, ConfigManager.GetMedianamikCultures().Select(c => c.CultureInfo)); 
            }
            chkGroups.DataBind();

            
        }
                

        protected bool IsNew
        {
            get { return string.IsNullOrEmpty(RoleParam) || !Roles.RoleExists(RoleParam); }
        }


        protected string RoleParam
        {
            get
            {
                return Request["name"] ?? string.Empty;
            }
        }
    }
}
